extends Area2D

@export var playerIndex = 0
@export var speed = 900  
@export var acceleration = 0.5

var target_y = position.y
var current_velocity = 0.0


func _physics_process(delta: float) -> void:
	var input = 0.0
	match playerIndex:
		0:
			if Input.is_action_pressed("p1_up"):
				input -= 1.0
			if Input.is_action_pressed("p1_down"):
				input += 1.0
		1:
			if Input.is_action_pressed("p2_up"):
				input -= 1.0
			if Input.is_action_pressed("p2_down"):
				input += 1.0
	
	if input != 0.0:
		print("input",input)
		target_y = position.y + input * speed * delta  # 直接计算目标位置
		target_y = clamp(target_y, 16, 632)
		position.y = lerp(position.y, float(target_y), acceleration)
				
	for i in get_overlapping_areas():
		if i.is_in_group("ball"):
			match playerIndex:
				0: i.vec.x = 8
				1: i.vec.x = -8	
				
